//
// Created by andryu on 2025/1/1.
//

#include "RectangleColorRender.h"
#include "ShapeRenderer.h"
#include "../utils/ShaderLoader.h"
#include <android/log.h>
#include <string>

#define LOG_TAG "LabelSimple"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

GLuint indices[] = {
        0, 1, 3, // 第一个三角形
        1, 2, 3  // 第二个三角形
};

RectangleColorRender::RectangleColorRender() {
    mVertices = {
            // positions          // colors
            0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f, // 右上角 红色
            0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f, // 右下角 绿色
            -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f, // 左下角 蓝色
            -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f  // 左上角 黄色
    };
}

//着色器代码
std::string RectangleColorRender::getFragmentShaderCode() const {
    return R"(
        #version 300 es
        precision mediump float;
        in vec4 vColor;
        out vec4 fragColor;
        void main(){
            fragColor = vColor;
        }
    )";
}

std::string RectangleColorRender::getVertexShaderCode() const {
    return R"(
        #version 300 es
        layout(location = 0) in vec4 vPosition;
        layout(location = 1) in vec4 aColor;
        out vec4 vColor;
        void main(){
            gl_Position = vPosition;
            vColor = aColor;
        }
    )";
}

void RectangleColorRender::onSurfaceCreated() {
    glClearColor(1.0,1.0,1.0,1.0);
    glClearDepthf(1.0);
    //启用深度测试
    glEnable(GL_DEPTH_TEST);
    //设置深度测试函数为“小于或等于”
    glDepthFunc(GL_LEQUAL);

    // 初始化着色器和程序对象
    GLuint vertexShader = ShaderLoader::loadShader(GL_VERTEX_SHADER, getVertexShaderCode().c_str());
    GLuint fragmentShader = ShaderLoader::loadShader(GL_FRAGMENT_SHADER, getFragmentShaderCode().c_str());
    mProgram = glCreateProgram();
    glAttachShader(mProgram, vertexShader);
    glAttachShader(mProgram, fragmentShader);
    glLinkProgram(mProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
}

void RectangleColorRender::onSurfaceChanged(jint width, jint height) {

}

void RectangleColorRender::onDrawFrame() {
    // 清屏
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // 获取属性位置
    GLint positionLoc = glGetAttribLocation(mProgram, "vPosition");
    GLint colorLoc = glGetAttribLocation(mProgram, "aColor");
    LOGD("CubeRenderer  positionLoc = %d, colorLoc = %d", positionLoc, colorLoc);
    // 创建缓冲区对象
    GLuint vbo, ibo;
    glGenBuffers(1, &vbo);
    glGenBuffers(1, &ibo);

    // 加载顶点数据到VBO
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    if (!mVertices.empty()) {
        glBufferData(GL_ARRAY_BUFFER,
                     mVertices.size() * sizeof(GLfloat), // 注意这里使用了 size() 和 sizeof
                     mVertices.data(),                   // 使用 data() 方法获取 C 风格数组指针
                     GL_STATIC_DRAW);                    // 根据使用情况选择适当的绘制模式
    }

    // 加载索引数据到IBO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // 启用顶点属性数组
    glEnableVertexAttribArray(positionLoc);
    glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)0);
    glEnableVertexAttribArray(colorLoc);
    glVertexAttribPointer(colorLoc, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

    // 使用程序对象
    glUseProgram(mProgram);
    // 绘制四边形
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

    // 解绑缓冲区
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(positionLoc);
    glDisableVertexAttribArray(colorLoc);
}